<?php

namespace App\Controller\Admin;

use App\Controller\AdminCommon\GradeCategoryCommonController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use App\Base\BaseController;
use App\Base\BaseController4Admin;
use App\Entity\GradeCategory as Entity;
use App\Setting\GradeCategorySetting;

/**
 * @route("/admin/")
 */
class GradeCategoryController extends BaseController4Admin
{

    /**
     * @route("GradeCategory/add",name="GradeCategoryPreAdd")
     */
    public function add()
    {
        $uid = $this->needToBeAdmin();
        if ( $parent = $this->getParamRequired("parent")) {
            $entity = new Entity();
            $entity->setParent($parent);
        }else{
            $entity = null;
        }
        $this->setSetting(new GradeCategorySetting($this->getDoctrine()));
        if (!$entity) {$entity = new $this->entitySetting->entityPath();}
        $setting = new GradeCategorySetting($this->getDoctrine());
        $from = $this->getFormCreator()->createAddFrom($entity,$setting->formConfig(),$this->generateUrl("GradeCategoryDoAdd"));
        $data = array(
            "title" => $this->entitySetting->entityCnName,
            "show"   => $from->createView(),
            "isAdd"   => true,
        );
        return $this->render("Admin/GradeCategory/cat_edit.php.twig",$data);
    }

    /**
     * @route("GradeCategory/add/do",name="GradeCategoryDoAdd")
     *
     *
     */
    public function doAdd(Request $request)
    {
        $uid = $this->needToBeAdmin();
        $entity = new Entity();
        $parent = $this->getParamRequired("form");
        $rep = $this->getDoctrine()->getRepository("App:GradeCategory");
        $parentEntity = $rep->find($parent["parent"]);
        if ($parentEntity) {
            $level = $parentEntity->getLevel() + 1;
        } else {
            $level = 0;
        }
        $this->setSetting(new GradeCategorySetting($this->getDoctrine()));
        $setting = new GradeCategorySetting($this->getDoctrine());
        if ($entity = $this->getFormCreator()->fillEntity($entity,$request,$setting->formConfig())) {
            $entity->setLevel($level);
            $this->myEm()->persist($entity);$this->myEm()->flush();
            $urlData = [];//传参
            return $this->js_out("保存成功",$this->generateUrl("GradeCategoryLists",$urlData));
        } else {
            return $this->js_out("系统错误，请联系管理员",$this->generateUrl("GradeCategoryPreAdd"));
        }
    }

    /**
     * @route("GradeCategory/edit/{id}",name="GradeCategoryPreEdit")
     */
    public function edit(Entity $entity)
    {
        $uid = $this->needToBeAdmin();
        $this->setSetting(new GradeCategorySetting($this->getDoctrine()));
        $setting = new GradeCategorySetting($this->getDoctrine());
        $from = $this->getFormCreator()->createAddFrom($entity,$setting->formConfig(),$this->generateUrl("GradeCategoryDoEdit",["id"=>$entity->getId()]));
        $data = array(
            "title" => $this->entitySetting->entityCnName,
            "show"   => $from->createView(),
            "isAdd"   => false,
        );
        return $this->render("Admin/GradeCategory/cat_edit.php.twig",$data);
    }

    /**
     * @route("GradeCategory/edit/{id}/do",name="GradeCategoryDoEdit")
     *
     *
     */
    public function doEdit(Request $request,Entity $entity)
    {
        $uid = $this->needToBeAdmin();
        //私有代码
        $parent = $request->get("form");
        $rep = $this->getDoctrine()->getRepository("App:GradeCategory");
        $parentEntity = $rep->find($parent["parent"]);
        if ($parentEntity) {
            $level = $parentEntity->getLevel() + 1;
        } else {
            $level = 0;
        }
        $this->setSetting(new GradeCategorySetting($this->getDoctrine()));
        $setting = new GradeCategorySetting($this->getDoctrine());
        if ($entity = $this->getFormCreator()->fillEntity($entity,$request,$setting->formConfig())) {
            $entity->setLevel($level);
            $this->myEm()->persist($entity);$this->myEm()->flush();
            $urlData = [];
            return $this->js_out("保存成功",$this->generateUrl("GradeCategoryLists",$urlData));
        } else {
            return $this->js_out("系统错误，请联系管理员",$this->generateUrl("GradeCategoryPreEdit",array("id"=>$entity->getId())));
        }
    }

    /**
     * @route("GradeCategory",name="GradeCategoryLists")
     */
    public function lists()
    {
        $uid = $this->needToBeAdmin();
        $this->setSetting(new GradeCategorySetting($this->getDoctrine()));
        //return $this->comCatLists();
        $lists = $this->getLists(0,0);
        $table = str_replace("_","",$this->entitySetting->table);
//        $table = $this->entitySetting->entity;
        $data = $this->buildeCatTree($lists["data"],$table);
        /*URL配置*/
        $urls = array(
            "toolUrl"=>$this->generateUrl($this->entitySetting->entityName."Tools"),
            "addUrl"=>$this->generateUrl($this->entitySetting->entityName."PreAdd"),
        );
        $rep = $this->getDoctrine()->getManager()->getRepository($this->entitySetting->bundleName.":".$this->entitySetting->entityName);
        $classes = $rep->findAll();

        $entityCnName = $this->entitySetting->entityCnName;
        return $this->render("Admin/GradeCategory/cat_list.php.twig", array(
            'title'     => "$entityCnName 列表",
            "classes"   => $classes,
            'urls'      => $urls,
            'lists'     => $data,
        ));
    }


    /**
     * @route("GradeCategory/tools",name="GradeCategoryTools")
     */
    public function tools(Request $request)
    {
        $uid = $this->needToBeAdmin();
        $this->setSetting(new GradeCategorySetting($this->getDoctrine()));
        return $this->comTools($request);
    }

}
